package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/22 15:06
 * @Version 1.0
 */

@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加好友
    @Override
    public void addFriend(Long userId, Long friendId) {

        //1.删除之前的关心
        //你删他
        mongoTemplate.remove(new Query(
                Criteria.where("userId").is(userId)
                        .and("friendId").is(friendId)), Friend.class);

        //他删你
        mongoTemplate.remove(new Query(
                Criteria.where("userId").is(friendId)
                        .and("friendId").is(userId)), Friend.class);

        //2.删除对方动态
        //你删他
        mongoTemplate.remove(new Query(Criteria.where("userId").is(friendId)), ConstantUtil.MOVEMENT_FRIEND + userId);
        //他删你
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)), ConstantUtil.MOVEMENT_FRIEND + friendId);

        //3.添加
        //你添加他好友
        Friend friend1 = new Friend();

        friend1.setUserId(userId);
        friend1.setFriendId(friendId);
        friend1.setCreated(System.currentTimeMillis());

        mongoTemplate.save(friend1);

        //你添加他动态
        List<Movement> movementList1 = mongoTemplate.find(new Query(Criteria.where("userId").is(friendId)), Movement.class);
        if (CollectionUtil.isNotEmpty(movementList1)) {

            for (Movement movement : movementList1) {

                //动态添加到自己的好友动态表中
                FriendMovement friendMovement1 = new FriendMovement();
                friendMovement1.setCreated(movement.getCreated());
                friendMovement1.setUserId(movement.getUserId());
                friendMovement1.setPublishId(movement.getId());

                mongoTemplate.save(friendMovement1, ConstantUtil.MOVEMENT_FRIEND + userId);
            }
        }

        //他加你好友
        Friend friend2 = new Friend();
        friend2.setUserId(friendId);
        friend2.setFriendId(userId);
        friend2.setCreated(System.currentTimeMillis());

        mongoTemplate.save(friend2);
        //他添加你的动态，到好友动态表中
        //查找你的动态
        List<Movement> movementList2 = mongoTemplate.find(new Query(Criteria.where("userId").is(userId)), Movement.class);
        if (CollectionUtil.isNotEmpty(movementList2)) {

            for (Movement movement : movementList2) {

                FriendMovement friendMovement2 = new FriendMovement();
                friendMovement2.setUserId(movement.getUserId());
                friendMovement2.setPublishId(movement.getId());
                friendMovement2.setCreated(movement.getCreated());

                mongoTemplate.save(friendMovement2, ConstantUtil.MOVEMENT_FRIEND + friendId);
            }

        }

    }

    //查询好友
    @Override
    public PageBeanVo findFriendByPage(Long userId, Integer pageNum, Integer pageSize) {

        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).skip((pageNum - 1) * pageSize).limit(pageSize);

        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        return new PageBeanVo(pageNum, pageSize, 0L, friendList);


    }
}




























